﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Acoris.Markdown
{
    /// <summary>
    /// 表示一个AML项
    /// </summary>
    internal class AMLItem:AMLParseNode
    {
        private AMLProperty     m_property;
        private AMLValue        m_value;

        internal AMLItem(
            AMLParseNode parent,
            int offset,
            int length,
            string document,
            int nodeNameOffset,
            int nodeNameLength,
            int valueOffset,
            int valueLength):
            base(parent, AMLNodeType.Item,offset,length,document,nodeNameOffset,nodeNameLength,valueOffset,valueLength)
        {
            m_property = new AMLProperty(document.Substring(nodeNameOffset, nodeNameLength));
            m_value = new AMLValue();
            m_value.GetValue(document.Substring(valueOffset, valueLength));
        }

        /// <summary>
        /// 获取当前AML项的属性值
        /// </summary>
        public AMLProperty Property
        {
            get { return m_property; }
        }

        /// <summary>
        /// 获取当前AML项的值
        /// </summary>
        public AMLValue Value
        {
            get { return m_value; }
        }

        /// <summary>
        /// 获取两个对象之间的等价性
        /// </summary>
        /// <param name="obj">指定要比较的对象</param>
        /// <returns>如果两个对象等价性一直则返回true，否则返回false</returns>
        public override bool Equals(object obj)
        {
            if(obj is AMLItem item)
            {
                return item.m_property == this.m_property && item.m_value == this.m_value;
            }
            return false;
        }

        /// <summary>
        /// 获取当前对象的哈希值
        /// </summary>
        /// <returns>返回当前对象的哈希值</returns>
        public override int GetHashCode()
        {
            return m_property.GetHashCode() ^ m_value.GetHashCode();
        }

        /// <summary>
        /// 获取与当前对象等价的字符串形式
        /// </summary>
        /// <returns>返回与当前对象等价的字符串形式</returns>
        public override string ToString()
        {
            return $"<{m_property.Name},value:{m_value.Count}>";
        }
    }
}
